var orderDetail = JSON.parse(sessionStorage.getItem("orderDetailall"));
getBankInfo()
function getBankInfo() {
	$.ajax({
		type: "POST",
		url: root.orderInter.getBankInfo,
		cache: false, //禁用缓存
		dataType: "json",
		data: {
			order_id: orderDetail.orderBase.order_id, // 订单编号
		},
		success: function (result) {
			$("#accountNamess").html(result.data.accountName)
			$("#bankNames").html(result.data.bankName)
			$("#accountNumbers").html(result.data.accountNumber)
			$("#lineNumber").html(result.data.lineNumber)
			$("#paidAmount").html(Number(result.data.paidAmount).toFixed(2))

			$("#accountName").html(result.data.accountName)
			$("#bankAccount").html(result.data.accountNumber)
			$("#bankOfdeposit").html(result.data.bankName)
			$("#interBanknumber").html(result.data.lineNumber)
			$("#actual_amount").val(Number(result.data.paidAmount).toFixed(2))
			//差额
			var differences=Number($("#actual_amount").val())-Number($("#paidAmount").html())
			$("#difference").html(differences.toFixed(2))

			$("#due_account_id").html(result.data.accountId)
			$("#due_company_code").html(result.data.companyCode)
			$("#actual_account_id").html(result.data.accountId)
			$("#actual_company_code").html(result.data.companyCode)


		},
		error: function () {
			$.Huimodalalert('失败！',2000);
		}
	});
}
confirmCollection()
function confirmCollection() {
	$.ajax({
		url:root.orderInter.getAllBank,
		cache: false,  //禁用缓存
		type: "POST",
		dataType:'json',
		contentType: "application/json",
		data: JSON.stringify({
			pageNum:1,
			pageSize:5,
			"bankName" :$("#bankName").val().trim(),
			"accountName":$("#accountNames").val().trim(),
			"accountNumber":$("#accountNumber").val().trim()

		}),
		success: function (result) {
			confirmCollectiontable(result);
			var totalPages=0
			if(result.data!=null){
				totalPages=result.data.totalPage
			}else {
				totalPages=0
			}
			$('.M-box3').pagination({
				pageCount: totalPages,
				jump: true,
				coping: true,
				prevContent: '上页',
				nextContent: '下页',
				callback: function (api) {
					$.ajax({
						url: root.orderInter.getAllBank,
						cache: false,  //禁用缓存
						type: "POST",
						dataType:'json',
						contentType: "application/json",
						data: JSON.stringify({
							pageNum:api.getCurrent(),
							pageSize:5,
							"bankName" :$("#bankName").val().trim(),
							"accountName":$("#accountNames").val().trim(),
							"accountNumber":$("#accountNumber").val().trim()

						}),
						success: function (result) {
							confirmCollectiontable(result);
						},
						error: function (XMLHttpRequest, textStatus, errorThrown) {
							alert(XMLHttpRequest);
						}
					});
				}
			});

		},
		error: function () {
			$.Huimodalalert('失败！',2000);
		}
	});
}
/*表格渲染*/
var dataSource = new Array();
//table的数据源
var table;   //table对象
dataSource = [];
function confirmCollectiontable(data) {
	dataSource.splice(0, dataSource.length);//清空数组
	if (data.data!=""&&data.data!=null) {
		$.each(data.data.result, function (i, field) {
			var rowData = new Array();
			rowData.push(field.accountName);
			rowData.push(field.accountNumber);
			rowData.push(field.bankName);
			rowData.push(field.lineNumber);
			rowData.push(field.accountId);
			rowData.push(field.companyCode);
			rowData.push(field.subCompanyName);
			dataSource.push(rowData);
		});
	}
	initDataTable(dataSource);

	function initDataTable(dataSource) {
		var dttable = $('#confirmCollectiontable').dataTable(); // 初始化dataTable，清空上一个值
		dttable.fnClearTable(); //清空一下table
		dttable.fnDestroy(); //还原初始化了的datatable
		$("#confirmCollectiontable").DataTable({
			"order": [[ 1, "desc" ]],
			"autoWidth": false,
			"ordering": false,
			"searching": false,
			"paging": false,
			"retrieve": true,
			"destroy": true,
			"bAutoWidth": true,
			"bLengthChange": true,
			"aLengthMenu": [5, 10, 25, 50],
			"oLanguage": {
				"sLengthMenu": "每页显示 _MENU_ 条记录",
				"sZeroRecords": "抱歉， 没有找到",
				"sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
				"sInfoEmpty": "",
				"sInfoFiltered": "(从 _MAX_ 条数据中检索)",
				"oPaginate": {
					"sFirst": "首页",
					"sPrevious": "前一页",
					"sNext": "后一页",
					"sLast": "尾页"
				},
				"sZeroRecords": "没有检索到数据",
				"sProcessing": "<img src='./loading.gif' />",
			},
			data: dataSource,
			columnDefs: [
				{
					"visible": true,
					"targets": 0
				},
				{
					"render": function(data, type, row, meta) {
						var val = '<span style="text-align: left;"><td><input class="spuId" type="radio" name="informations" onclick="selectInformation(this,\''+row[4]+'\',\''+row[5]+'\')"></td></span>';
						return val;
					},
					"targets": 0
				},
				{
					"render": function (data, type, row, meta) {
						var val = row[0]
						if (val == undefined) {
							val = ''
						}
						return val;
					},
					"targets": 1
				},
				{
					"render": function (data, type, row, meta) {
						var val = row[1]
						if (val == undefined) {
							val = ''
						}
						return val;
					},
					"targets": 2
				},
				{
					"render": function (data, type, row, meta) {
						var val = row[2]
						if (val == undefined) {
							val = ''
						}
						return val;
					},
					"targets": 3
				},

				{
					"render": function (data, type, row, meta) {
						var val = row[3]
						if (val == undefined) {
							val = ''
						}
						return val;
					},
					"targets": 4
				},
				{
					"render": function (data, type, row, meta) {
						var val = row[6]
						if (val == undefined) {
							val = ''
						}
						return val;
					},
					"targets": 5
				},

			],

		});
	};
	//权限控制函数
	power()
}
//选择信息
function selectInformation(val,id,code) {
	$("#accountName").html($($(val).parent().parent().siblings()[0]).html())
	$("#bankAccount").html($($(val).parent().parent().siblings()[1]).html())
	$("#bankOfdeposit").html($($(val).parent().parent().siblings()[2]).html())
	$("#interBanknumber").html($($(val).parent().parent().siblings()[3]).html())
	$("#actual_amount").val("")
	$("#actual_account_id").html(id)
	$("#actual_company_code").html(code)
	$("#difference").html("0.00")

}
//选择
function addBtn(cen) {
	$("#fileUp").trigger('click');
}
// 上传返现
function putDataToPage(e,can) {
	if($("#Informations").find(".shuju").length >9){
		layer.msg("选择凭证不能超过10", {icon: 5, time: 1000});
		return false;
	}
	var formData = new FormData();
	formData.append('file',$(can)[0].files[0]);
	$.ajax({
		url: root.inter.imgUpload,
		cache: false,  //禁用缓存
		type: "POST",
		// 告诉jQuery不要去处理发送的数据
		processData : false,
		// 告诉jQuery不要去设置Content-Type请求头
		contentType : false,
		data: formData,
		success: function (data) {
			$("#Informations").append('  <div class="shuju">\n' +
				'                                   <span style="display: none" class="fullPaths">'+data[0].fullPath+'</span> <span class="f-l formControls lh-26 ml-10 fileNames" style="max-width: 303px; ">'+data[0].fileName+'</span><a class="f-l lh-26" style="margin-left: 12px;" onclick="deleteInformation(this)">删除</a>\n' +
				'                                </div>')
			$("#fileUp").attr('type','text');
			$("#fileUp").attr('type','file');

		}
	})
}
//删除
function deleteInformation(val) {
		$(val).parent().remove()
}
// 取消
function cancelInformation(){
	// 关闭当前页面
	parent.layer.closeAll();
}
//确认
var isIng = false;
function confirmCollectionadd() {
	if(isIng){
		layer.msg('请求已提交，请勿重复点击！');
		return false;
	}
	isIng = true;


	if($("#actual_amount").val()  == ""){
		$.Huimodalalert('实到金额不能为空',2000);
		isIng = false;
		return false;
	}
	if(Number($("#actual_amount").val())  == "0"){
		$.Huimodalalert('实到金额不能为0',2000);
		isIng = false;
		return false;
	}
	var remarkText = $("#remarkText").val().replace(/\s+/g,"");
	if(remarkText == ""){
		$.Huimodalalert('备注不能为空',2000);
		isIng = false;
		return false;
	}
	var orderProof={}
	var orderProofarr=[]
	var Informations= $("#Informations").find(".shuju")
	for (var a=0;a<Informations.length;a++){

		orderProof={
			"proof_url":$(Informations[a]).find(".fullPaths").html(),
			"proof_name":$(Informations[a]).find(".fileNames").html()
		}
		orderProofarr.push(orderProof)

	}
	if($("#actual_company_code").html()!=$("#due_company_code").html()){
		$.Huimodalalert('所选实到账户与订单发货公司跨法人，请选择应到收款账户并备注说明',2000);
		isIng = false;
		return false;
	}
	$.ajax({
		contentType:"application/json",
		type: "POST",
		url: root.orderInter.order_comfirmMoney,
		cache: false,  //禁用缓存
		dataType: "json",
		data: JSON.stringify({
			bus_id:orderDetail.orderBase.order_id,//业务id
			operate_userid:JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.user_ID,//操作人id
			operate_username: JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name,//操作人名称
			//应到：
			due_account_name:$("#accountNamess").html(),
			due_account_number:$("#accountNumbers").html(),
			due_bank_name:$("#bankNames").html(),
			due_line_number:$("#lineNumber").html(),
			due_amount:$("#paidAmount").html(),
			due_account_id:$("#due_account_id").html(),
			due_company_code:$("#due_company_code").html(),

			//实到：
			actual_account_name:$("#accountName").html(),
			actual_account_number:$("#bankAccount").html(),
			actual_bank_name:$("#bankOfdeposit").html(),
			actual_line_number:$("#interBanknumber").html(),
			actual_amount:$("#actual_amount").val(),
			remark: $("#remarkText").val(),//备注信息
			orderProofList:orderProofarr,
			actual_account_id:$("#actual_account_id").html(),
			actual_company_code:$("#actual_company_code").html(),

		}),
		success: function (result) {
			if(result.code == 0){
				setTimeout("ddf()",3000); //1秒后调用fun函数
				layer.msg("确认收款成功",{icon: 6,time:2000},function () {
					var index =  parent.location.reload();
					parent.layer.close(index);
				});
			}else {
				layer.msg(result.message,{icon: 5,time:2000},function () {
					var index =  parent.location.reload();
					parent.layer.close(index);
				});
			}

		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			isIng = false;
		}
	});
}
function ddf() {
	$.ajax({
		contentType:"application/json",
		type: "POST",
		url:'https://'+ window.location.host +'/api/admin/apromotion/lotteryDraw/orderPayAfterDraw',
		cache: false,  //禁用缓存
		dataType: "json",
		data: JSON.stringify({
			"memberId":sessionStorage.getItem("memberIds"),
			"customerType":"",
			"joinRequire": 20,
			"source": 10,
			"platform":sessionStorage.getItem("platforms"),
		}),
		success: function (result) {

		},
	});
}
function actualamount(obj) {
	obj.value = obj.value.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符
	obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
	obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
	obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');//只能输入两个小数
	//差额
	if($("#actual_amount").val()!=""){
		var differences=Number($("#actual_amount").val())-Number($("#paidAmount").html())
		$("#difference").html(differences.toFixed(2))
	}else {
		$("#difference").html("0.00")
	}

}